package com.simin.base.easy;


import androidx.recyclerview.widget.RecyclerView;

import com.simin.base.easy.adapter.MultiTypeAdapter;
import com.simin.base.easy.adapter.RecyclerArrayAdapter;


public class EasyDataObserver extends RecyclerView.AdapterDataObserver {
    private EasyRecyclerView recyclerView;
    private RecyclerArrayAdapter mArrayAdapter;
    private MultiTypeAdapter multiTypeAdapter;
    private boolean isInitialized = false;
    private boolean hasProgress;

    public EasyDataObserver(EasyRecyclerView recyclerView, boolean isLoading) {
        this.recyclerView = recyclerView;
        hasProgress = isLoading;
        if (recyclerView.getAdapter() instanceof RecyclerArrayAdapter) {
            mArrayAdapter = (RecyclerArrayAdapter) recyclerView.getAdapter();
        } else if (recyclerView.getAdapter() instanceof MultiTypeAdapter) {
            multiTypeAdapter = (MultiTypeAdapter) recyclerView.getAdapter();
        }
    }

    private boolean isHeaderFooter(int position) {
        if (mArrayAdapter != null) {
            return (position < mArrayAdapter.getHeaderCount() || position >= mArrayAdapter.getHeaderCount() + mArrayAdapter.getCount());
        }
        if (multiTypeAdapter != null) {
            return (position < multiTypeAdapter.getHeaderCount() || position >= multiTypeAdapter.getHeaderCount() + multiTypeAdapter.getCount());
        }
        return false;
    }

    @Override
    public void onItemRangeChanged(int positionStart, int itemCount) {
        super.onItemRangeChanged(positionStart, itemCount);
        if (!isHeaderFooter(positionStart)) {
            update();
        }
    }

    @Override
    public void onItemRangeInserted(int positionStart, int itemCount) {
        super.onItemRangeInserted(positionStart, itemCount);
        if (!isHeaderFooter(positionStart)) {
            update();
        }
    }

    @Override
    public void onItemRangeRemoved(int positionStart, int itemCount) {
        super.onItemRangeRemoved(positionStart, itemCount);
        if (!isHeaderFooter(positionStart)) {
            update();
        }
    }

    @Override
    public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
        super.onItemRangeMoved(fromPosition, toPosition, itemCount);
        update();//header&footer不会有移动操作
    }

    @Override
    public void onChanged() {
        super.onChanged();
        update();//header&footer不会引起changed
    }

    //自动更改Container的样式
    private void update() {
        if (recyclerView.getAdapter() instanceof RecyclerArrayAdapter) {
            if (((RecyclerArrayAdapter) recyclerView.getAdapter()).getCount() == 0) {
                if (hasProgress && !isInitialized) {
                    recyclerView.showPageLoading();
                } else {
                    recyclerView.showPageEmpty();
                }
            } else {
                recyclerView.showRecycler();
            }
        } else if (recyclerView.getAdapter() instanceof MultiTypeAdapter) {
            MultiTypeAdapter multiTypeAdapter = (MultiTypeAdapter) recyclerView.getAdapter();
            int dataCount = multiTypeAdapter.getCount();
            if (dataCount == 0) {
                recyclerView.showPageEmpty();
                /*if (hasProgress && !isInitialized) {
                    recyclerView.showPageLoading();
                } else {
                    recyclerView.showPageEmpty();
                }*/
            } else {
                recyclerView.showRecycler();
            }
        } else {
            if (recyclerView.getAdapter().getItemCount() == 0) {
                if (hasProgress && !isInitialized) {
                    recyclerView.showPageLoading();
                } else {
                    recyclerView.showPageEmpty();
                }
            } else {
                recyclerView.showRecycler();
            }
        }
        isInitialized = true;//设置Adapter时会有一次onChange。忽略此次。
    }
}